//
//  SPicDetails1ViewController.m
//  News
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SPicDetails1ViewController.h"
#import "UIImageView+WebCache.h"
#import "SNetWorkHandle.h"

@interface SPicDetails1ViewController ()<UIScrollViewDelegate, UITextViewDelegate>

@property(nonatomic ,assign)NSInteger a;
@property(nonatomic ,retain)NSArray *arr;
@property(nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property(nonatomic ,retain)UIImageView *imageView;
@property(nonatomic, retain)UITextView *textViewOne;
@property(nonatomic ,assign)CGFloat F;
@end

@implementation SPicDetails1ViewController
- (void)dealloc
{
    [_arr release];
    [_str release];
    [_setName release];
    [_dic release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];

    
    
    // 请求出现小菊花
    self.imageView  = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _imageView.image = [UIImage imageNamed:@"5EA2D64D-15BE-43D8-AB18-E15BCE039C8F"];
    _imageView.alpha =0.6;
    [self.view addSubview:_imageView];
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [_activityIndicator setCenter:_imageView.center];
    _activityIndicator.backgroundColor= [UIColor grayColor];
    [_activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [_imageView addSubview:_activityIndicator];
        [_activityIndicator startAnimating];
    
    
    [self details];
}
-(void)details
{

    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/0096/%@.json",_str];
    
    [SNetWorkHandle getDataWithURL:str completion:^(id result) {
        
        self.setName  = [result objectForKey:@"setname"];
        self.arr  = [result objectForKey:@"photos"];
        self.picObjectArr = [[NSMutableArray alloc] init];
        self.picWenZiArr = [[NSMutableArray alloc] init];
        self.PicWangzhiArr = [[NSMutableArray alloc] init];
        self.WenZiArr = [[NSMutableArray alloc] init];
        
        for (NSDictionary *temp in _arr) {
            
            self.picObject = [[SPic alloc] initWithDic:temp];
            [_picObjectArr addObject:_picObject];
            [_picWenZiArr addObject:_picObject.note];
            [_PicWangzhiArr addObject:_picObject.imgurl];
            [_WenZiArr addObject:_picObject.note];
            
                    }
        
        self.dic = result;
        [self create];
        [_imageView removeFromSuperview];
    }];
}

// 图片详情
-(void)create
{
    
    CGFloat f1 = [[self class]heightWithText:_WenZiArr[0]];
    
       self.scrollView  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 3.4 - 50, self.view.frame.size.width, self.view.frame.size.height / 3.4+ 50)];
    
    _scrollView.pagingEnabled  =YES;
    _scrollView.delegate = self;
    _scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scrollView];
 
    self.textViewOne = [[UITextView alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height * 2  - 40, self.view.frame.size.width ,f1 + 40)];
    
    _textViewOne.editable = NO;
    _textViewOne.textColor = [UIColor whiteColor];
    _textViewOne.text = _WenZiArr[0];
    _textViewOne.font = [UIFont systemFontOfSize:17];
    _textViewOne.backgroundColor =[UIColor blackColor];
    _textViewOne.delegate = self;
    [self.view addSubview:_textViewOne];
   

    for (NSInteger i = 0; i < _PicWangzhiArr.count; i++) {
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * i + self.view.frame.size.width, 0);
        
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.scrollView.bounds.size.height)];
        
    
        [img sd_setImageWithURL:[NSURL URLWithString:_PicWangzhiArr[i]]placeholderImage:[UIImage imageNamed:@"``_3(NZ4U{L@4L7FN0J70DA"]];
        [self.scrollView addSubview:img];
    }
     
        
    UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height * 2 - 80  , self.view.frame.size.width - 55 ,30)];

    lable1.text = _setName;
    
    lable1.textColor = [UIColor whiteColor];
    
    lable1.backgroundColor = [UIColor blackColor];
    [self.view addSubview:lable1];
    [lable1 release];
    
    
    self.ladelThree   = [[UILabel alloc] initWithFrame:CGRectMake(lable1.bounds.size.width  , _scrollView.frame.size.height * 2 - 80 , 55, 30)];
    
    _ladelThree.text = [NSString stringWithFormat:@"1/%ld", (unsigned long)_arr.count];
    
    _ladelThree.textAlignment = NSTextAlignmentRight;
    
    _ladelThree.textColor = [UIColor whiteColor];
    _ladelThree.backgroundColor = [UIColor blackColor];
    [self .view addSubview:_ladelThree];
    [_ladelThree release];
    
    
    
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView) {
        
        CGPoint pointOne = _scrollView.contentOffset;
        
        NSInteger a = (NSInteger)pointOne.x / self.view.frame.size.width;
        
        _textViewOne.text = _WenZiArr[a];
        
        self.F = [[self class] heightWithText:_textViewOne.text];
        _textViewOne.frame = CGRectMake(0, _scrollView.frame.size.height * 2 - 40, self.view.frame.size.width, _F + 20);
        
        _ladelThree.text = [NSString stringWithFormat:@"%d/%ld", a + 1,(unsigned long)_arr.count];
    }
}


+(CGFloat)heightWithText:(NSString*)text
{
    //    设置一个字典保存文本属性
    
    NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    
    //    预设一个尺寸 文本最大不会超过这个尺寸
    CGSize size = CGSizeMake(375, 1000);
    
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
    
    //    返回得到的高度
    return rect.size.height;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
